layui.use(['layer','upload','laydate','layedit','treeSelect','table','form','laytpl','element'], function(){
    var $ = layui.$
        ,layer = layui.layer
        ,admin = layui.admin
        ,table = layui.table
        ,element = layui.element
        ,form = layui.form
        ,laytpl = layui.laytpl;
    var delFlag = null;
    var treeSelect= layui.treeSelect;//树形下拉框
    var upload = layui.upload;
    var layedit = layui.layedit;
    var laydate = layui.laydate;

    //获取路径参数
    var request = GetRequest();
    $("#id").val(request.id);
    $.get("/process/qrcode/" + request.id,function (data) {
        if (data.tableDefaultValue != null && data.tableDefaultValue != '') {
            var defaultValueObj = JSON.parse(data.tableDefaultValue);
            $("#name").val(defaultValueObj.name);
            $("#weight").val(defaultValueObj.weight);
            $("#responsiblePerson").val(defaultValueObj.responsiblePerson);
            $("#remarks").val(defaultValueObj.remarks);

            laydate.render({
                elem: '#investTime'
                ,value: new Date(defaultValueObj.investTime) //必须遵循format参数设定的格式
            });

            //加载投入品列表
            $.get("/invest/list",function (invest) {
                $.each(invest.data,function (i,ele) {
                    if (ele.id == defaultValueObj.investId) {
                        $("#investId").append('<option selected="selected" value="'+ ele.id +'">'+ ele.name +'</option>');
                    } else {
                        $("#investId").append('<option value="'+ ele.id +'">'+ ele.name +'</option>');
                    }
                });
                form.render('select');
                //同步投入品名称
                form.on('select(investId)',function (data) {
                    $.get("/invest/" + data.value,function (data) {
                        $("#name").val(data.name);
                    });
                });
            });
        } else {
            //加载投入品列表
            $.get("/invest/list",function (data) {
                $.each(data.data,function (i,ele) {
                    $("#investId").append('<option value="'+ ele.id +'">'+ ele.name +'</option>');
                });
                form.render('select');
                //同步投入品名称
                form.on('select(investId)',function (data) {
                    $.get("/invest/" + data.value,function (data) {
                        $("#name").val(data.name);
                    });
                });
            });

            //时间组件
            laydate.render({
                elem: '#investTime'
            });
        }
    });

    //添加企业信息
    form.on('submit(add)', function(data){
        //封装默认信息，遍历对象data.field
        var dataObj = {};
        dataObj.id = data.field.id;
        var tableDefaultValue = {};
        for (var key in data.field) {
            if (key != 'id') {
                tableDefaultValue[key] = data.field[key];
            }
        }
        dataObj.tableDefaultValue = JSON.stringify(tableDefaultValue);

        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});

        //初始化数据
        $.ajax({
            url: '/process/qrcode/edit',
            data: dataObj,
            type: 'POST',
            async: false,
            success: function (data) {
                if(data.code == 200) {
                    top.layer.close(index);
                    top.layer.msg("修改成功！");
                    layer.closeAll("iframe");
                    //刷新父页面
                    parent.location.reload();
                } else {
                    top.layer.close(index);
                    top.layer.msg("修改失败！");
                }
            },
            error:function () {
                top.layer.close(index);
                top.layer.msg("修改失败！");
            }
        });
        return false;
    });

    /**
     * 获取路径中的参数
     * @returns {Object}
     */
    function GetRequest() {
        var url = location.search;         //获取url中"?"符后的字串
        var theRequest = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(1);
            var strs = str.split("&");
            for (var i = 0; i < strs.length; i++) {
                theRequest[strs[i].split("=")[0]] = decodeURIComponent(strs[i].split("=")[1]);
            }
        }
        return theRequest;
    }

});

